<?php  
namespace App\Http\Controllers; 
require_once ('../app/alipay/AopSdk.php');  
  
class AlipayController  
{  
    /** 
     * 应用ID 
     */  
    const APPID = '2018052860271109';  
    /** 
     *请填写开发者私钥去头去尾去回车，一行字符串 
     */  
    const RSA_PRIVATE_KEY = 'MIIEogIBAAKCAQEApv0/3UzexQ6b827W/zYYeAAqiXAO/KHrJfdIZbazHshOpLdIIrk9XFZXGI+bZlgPFaTZsiyOidhjUj2NIx/uz3IW2d128e2EbUz5qs7DlhkZ3KvBcRRDGTgxUUaJHrail866VXZEyDPihmApz6lV2Tsyx712FroDfxnE1OB4rSOby/1BWuHTRrE5Zsv+2K8QfRDUOaiM5dcKjYe5CIJJbnXUYf5QoKdwEGd37Ao3be04+sQnRUsS7qh/egJRLQgOgbXGwZBaq7PRzarg3zsfdxsw5wfZbLerfwYsnEGdVR5GOBnRIPqNFnOWyZok+coJ3PD21v9UrKIss5XxStyrYwIDAQABAoIBAHuuPssN0EFrnJ4kjiDgVCctGFRLJw5+bsfj6z9KnkhCW90r0sXFJWlqmX+bfaHDvwhs3MBWuAvotUeIRyyozhvfiIU8vohZPyglRxX3pm1ZkgobPF8gfsIDGKP19kFFTA/cEa6StkcCBtk4CD/CT89hRS8jaH9UrppI37w7taWl85gBEmiQSjRYVZg1YjxIj6RY/ucbvwIlOzhQMvo/3kUJQ4vep16e/vbuFCi5boMGt/PXlWUh+HDqmYqnrE1WpCITHRL+jmYgK2li/oj985X//ZxkGTWSash8wWWD8RDOK7KAqOeJ6Bdy3DWI+DZA7flPIe/P/rdlr4DWWHcs9kECgYEA1mgqLyLhWL0irqkHwNGWtgTg6u/30oaQJWC/VYVv/JLCcKOIG2JjuGYpm2rn8PAsSOqmDkdfhTlLWEW5CqPG71E0NmkHb4eYMQ+ryEx+HXvq3Vo386fE/OFmy8jz6QW3ruQr586PYMfARW9sFy9CWbDoyfVKywwFVrjt5iO1bdkCgYEAx2I9IQ/MvJXn2io0ljNBYrEvaK+iI7c0LQUP4wZApTl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y4cjBerfskVDqCFDsoB3yUfU0KXhBhdd4+MDwIDAQAB';  
    /** 
     * 支付宝服务器主动通知商户服务器里指定的页面 
     * @var string 
     */  
    private $callback = "https://b.shidaichuxing.com/api/setorder/notify.php";  
  
    /** 
     *生成APP支付订单信息 
     * @param string $orderId   商品订单ID 
     * @param string $subject   支付商品的标题 
     * @param string $body      支付商品描述 
     * @param float $pre_price  商品总支付金额 
     * @param int $expire       支付交易时间 
     * @return bool|string  返回支付宝签名后订单信息，否则返回false 
     */  
    public function unifiedorder($orderId, $subject,$body,$pre_price,$expire){  
        try{  
            $aop = new \AopClient();  
            $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";  
            $aop->appId = self::APPID;  
            $aop->rsaPrivateKey = self::RSA_PRIVATE_KEY;  
            $aop->format = "json";  
            $aop->charset = "UTF-8";  
            $aop->signType = "RSA2";  
            $aop->alipayrsaPublicKey = self::ALIPAY_RSA_PUBLIC_KEY;  
            //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay  
            $request = new \AlipayTradeAppPayRequest();  
            //SDK已经封装掉了公共参数，这里只需要传入业务参数  
            $bizcontent = "{\"body\":\"{$body}\","      //支付商品描述  
                . "\"subject\":\"{$subject}\","        //支付商品的标题  
                . "\"out_trade_no\":\"{$orderId}\","   //商户网站唯一订单号  
                . "\"timeout_express\":\"{$expire}m\","       //该笔订单允许的最晚付款时间，逾期将关闭交易  
                . "\"total_amount\":\"{$pre_price}\"," //订单总金额，单位为元，精确到小数点后两位，取值范围[0.01,100000000]  
                . "\"product_code\":\"QUICK_MSECURITY_PAY\""  
                . "}";  
            $request->setNotifyUrl($this->callback);  
            $request->setBizContent($bizcontent);  
            //这里和普通的接口调用不同，使用的是sdkExecute  
            $response = $aop->sdkExecute($request);  
            //htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义，实际打印到日志以及http传输不会有这个问题  
            return htmlspecialchars($response);//就是orderString 可以直接给客户端请求，无需再做处理。  
        }catch (\Exception $e){  
            return false;  
        }  
  
    }  
}  